﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{

    class IPRANNeCfg
    {
#region 定义接口和数据类型转换
        [DllImport("NetCfg.dll")]
        private static extern int add(int a, int b);
        [DllImport("NetCfg.dll")]
        private static extern bool NetCfg_Init();
        [DllImport("NetCfg.dll")]
        private static extern bool getAllNe(out IntPtr pStr);
        [DllImport("NetCfg.dll")]
        private static extern bool AotoSetLogicPort(int item_status, string strIP, int maskcode);
        [DllImport("NetCfg.dll")]
        private static extern bool AutoSetLoopBackPort(int itemStatus, string strIP, int maskcode);
        [DllImport("NetCfg.dll")]
        private static extern bool setAllItemStatus(int type);
        [DllImport("NetCfg.dll")]
        private static extern bool getTopoLink(out IntPtr strTopolink);
        [DllImport("NetCfg.dll")]
        private static extern bool getNeLogicPort(int neid, out IntPtr pstrPorts);
        [DllImport("NetCfg.dll")]
        private static extern bool setNeLogicPort(int neid, string strPorts);
        [DllImport("NetCfg.dll")]
        private static extern bool getNeLoopBackPort(int neid, out IntPtr pstrPort);
        [DllImport("NetCfg.dll")]
        public static extern bool setNeLoopBackPort(int neid, string strPort);
        private List<ne> str2ne(string strne)
        {
            List<ne> resNe=new List<ne>();   
            string[] strtmp = strne.Split('!');
            foreach (string str in strtmp)
            {

                string[] strtmp2=str.Split(',');
                if (strtmp2.Count()!=3)
                {
                    throw new Exception(str+"转换有问题。");
                }
                ne netmp;
                netmp.neid = Int32.Parse(strtmp2[0]);
                netmp.nename = strtmp2[1];
                netmp.netype = (enum_netype)(Int32.Parse(strtmp2[2]));
                resNe.Add(netmp);
            }
            return resNe;
        }
        private List<topolink> str2TopoLink(string strTopoLink)
        {
            List<topolink> ltp = new List<topolink>();
            string[] strTLArray = strTopoLink.Split('!');
            foreach (string strTP in strTLArray)
            {
                if(strTP=="")
                    continue;
                string[] strtmp = strTP.Split(',');
                 if (strtmp.Count()!=10)
                {
                    throw new Exception(strTP + "转换有问题。");
                }
                topolink tl;
                tl.src.neid = Int32.Parse(strtmp[0]);
                tl.src.nename = strtmp[1];
                tl.src.netype=(enum_netype)(Int32.Parse(strtmp[2]));
                tl.src.portstr = strtmp[3];
                tl.src.strIP = strtmp[4];
                tl.dst.neid = Int32.Parse(strtmp[5]);
                tl.dst.nename = strtmp[6];
                tl.dst.netype = (enum_netype)(Int32.Parse(strtmp[7]));
                tl.dst.portstr = strtmp[8];
                tl.dst.strIP = strtmp[9];
                ltp.Add(tl);
            }
            return ltp;
        }
        private Logicport str2LogicPort(string strlp)
        {
            string[] strlpArray = strlp.Split(',');
            if (strlpArray.Count()!=13)
            {
                throw new Exception(strlp + "转换有问题。");
            }
            Logicport lp=new Logicport();
            lp.itemStatus=(enum_itemStatus)(Int32.Parse(strlpArray[0]));
            lp.portType = (enum_portType)(Int32.Parse(strlpArray[1]));
            lp.portSpeed = (enum_PortSpeed)(Int32.Parse(strlpArray[2]));
            lp.boardNum = Int32.Parse(strlpArray[3]);
            lp.portNum = Int32.Parse(strlpArray[4]);
            lp.portID = Int32.Parse(strlpArray[5]);
            lp.vlan = Int32.Parse(strlpArray[6]);
            lp.mainBoardNum = Int32.Parse(strlpArray[7]);
            lp.portMode = (enum_portMode)(Int32.Parse(strlpArray[8]));
            lp.ipmode = (enum_ipmode)(Int32.Parse(strlpArray[9]));
            lp.ipaddr=strlpArray[10];
            lp.maskcode = Int32.Parse(strlpArray[11]);
            lp.proEnable = (enum_proEnable)(Int32.Parse(strlpArray[12]));
            return lp;
        }
        private string LogicPort2str(Logicport lp)
        {
            string res;
            res = ((int)lp.itemStatus).ToString() + "," +
                ((int)lp.portType).ToString() + "," +
               ((int)lp.portSpeed).ToString() + "," +
                lp.boardNum.ToString() + "," +
                lp.portNum.ToString() + "," +
                lp.portID.ToString() + "," +
                lp.vlan.ToString() + "," +
               lp.mainBoardNum.ToString() + "," +
                 ((int)lp.portMode).ToString() + "," +
                 ((int)lp.ipmode).ToString() + "," +
                lp.ipaddr + "," +
                lp.maskcode.ToString() + "," +
                ((int)lp.proEnable).ToString();
            return res;
        }
        private loopbackPort str2LoopbackPort(string slbp)
        {
            string[] strlbpArray = slbp.Split(',');
            if (strlbpArray.Count() != 4)
            {
                throw new Exception(slbp + "转换有问题。");
            }
            loopbackPort lbp = new loopbackPort();
            lbp.neid = Int32.Parse(strlbpArray[0]);
            lbp.itemstatus = (enum_itemStatus)(Int32.Parse(strlbpArray[1]));
            lbp.strIP = strlbpArray[2];
            lbp.maskcode = Int32.Parse(strlbpArray[3]);
            return lbp;
        }
        private string loopbackPort2str(loopbackPort lbp)
        {
            string res;
            res = lbp.neid.ToString() + "," +
           ((int)lbp.itemstatus).ToString() + "," +
            lbp.strIP + "," +
            lbp.maskcode.ToString();
            return res;
        }
#endregion
       // private int nInit = 0;//指示类是否初始化
        public IPRANNeCfg()
        {
            NetCfg_Init();
           // nInit = 1;
        }
        public bool IPRAN_GetAllNe(out List<ne>  listNe)
        {
            IntPtr pStr;
            if (!getAllNe(out pStr))
            {
                listNe=new  List<ne>();
                return false;
            }
            listNe = str2ne(Marshal.PtrToStringAnsi(pStr));
            return true;
        }
        public bool IPRAN_SetAllItemStatus(enum_itemStatus itemStatus)
        {
            return setAllItemStatus((int)itemStatus);
        }
        public bool IPRAN_AotoSetLogicPort(int item_status, string strIP, int maskcode)
        {
            return AotoSetLogicPort(item_status,  strIP,  maskcode);
        }
        public bool IPRAN_AutoSetLoopBackPort(int itemStatus, string strIP, int maskcode)
        {
            return AutoSetLoopBackPort(itemStatus, strIP, maskcode);
        }
        public bool IPRAN_GetTopoLink(out List<topolink> ltp)
        {
            IntPtr pStr;
            if (!getTopoLink(out pStr))
            {
                ltp = new List<topolink>();
                return false;
            }
            ltp = str2TopoLink(Marshal.PtrToStringAnsi(pStr));
            return true;
        }
        public bool IPRAN_GetNeLogicPort(int neid, out Logicport lp)
        {
            IntPtr pStr;
            if (!getNeLogicPort(neid,out pStr))
            {
                lp = new Logicport();
                return false;
            }
            lp = str2LogicPort(Marshal.PtrToStringAnsi(pStr));
            return true;
        }
        public bool IPRAN_SetNeLogicPort(int neid, Logicport lp)
        {
            string slp = LogicPort2str(lp);
            return setNeLogicPort(neid, slp);
        }
        public bool IPRAN_GetNeLoopBackPort(int neid, out loopbackPort lp)
        {
            IntPtr pStr;
            if (!getNeLoopBackPort(neid,out pStr))
            {
                lp = new loopbackPort();
                return false;
            }
            lp = str2LoopbackPort(Marshal.PtrToStringAnsi(pStr));
            return true;
        }
        public bool IPRAN_SetNeLoopBackPort(int neid, loopbackPort lbp)
        {
            string slbp = loopbackPort2str(lbp);
            return setNeLoopBackPort(neid, slbp);
        }
    }
#pragma warning disable 0169
    enum enum_netype
    {
        type_IPRAN = 0,
        type_PTN = 1
    };
    enum enum_PortSpeed
    {

        speed_XGE = 0,
        speed_GE = 1,
        speed_FE = 2
    };
    enum enum_itemStatus
    {
        status_complete = 0,
        status_notdownload = 1,
        status_delete = 2,
        status_modify = 3
    };
    enum enum_ipmode
    {
        ipmode_notSet = 0,
        ipmode_selfSet = 1
    };
    enum enum_proEnable
    {
        pro_notEnable = 0,//都不是能
        pro_oneEnable = 1,//mpls
        pro_threeEnable = 2//3个协议使能
    };
    enum enum_portType
    {
        port_VLAN = 0,
        port_QINQ = 1,
        lag_VLAN = 2,
        lag_QINQ = 3,
        l2_VPORT = 4,
        l3_VPORT = 5,
        port_SVLAN = 6
    };
    enum enum_portMode
    {
        portMode_L2 = 0,
        portMode_L3 = 1,
        portMode_TP = 2
    };
    struct topoNode
    {
        public int neid;
        public string nename;
        public enum_netype netype;
        public string portstr;
        public string strIP;
    };
    struct topolink
    {
        public topoNode src;
        public topoNode dst;
    };
    struct ne
    {
        public int neid;
        public string nename;
        public enum_netype netype;
    };
    enum enum_getNeType
    {
        allNE = 0,
        only_IPRAN = 1,
        only_PTN = 2
    }
    struct Logicport//定义逻辑端口struct
    {
        public enum_itemStatus itemStatus;
        public enum_portType portType;
        public enum_PortSpeed portSpeed;
        public int boardNum;
        public int portNum;
        public int portID;
        public int vlan;
        public enum_portMode portMode;
        public int mainBoardNum;
        public enum_ipmode ipmode;
        public string ipaddr;
        public int maskcode;
        public enum_proEnable proEnable;
    };
    struct loopbackPort
    {
        public int neid;
        public enum_itemStatus itemstatus;
        public string strIP;
        public int maskcode;
    };

}
